package jwdt.xadmin.work.service.impl;

import java.util.ArrayList;
import java.util.List;

import jwdt.common.bean.Tree;
import jwdt.common.dao.BaseDao;
import jwdt.xadmin.work.bean.TProject;
import jwdt.xadmin.work.service.ProjectManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.fastjson.JSON;

public class ProjectManageServiceImpl implements ProjectManageService{
	private BaseDao<TProject> baseDao;
	
	public void setBaseDao(BaseDao<TProject> baseDao) {
		this.baseDao = baseDao;
	}
	@Override
	public PageUtil doList(PageUtil pu,TProject obj) throws Exception{
		String where = "";
		Integer fid = obj.getFid();
		if(fid!=null && fid != 0){
			where = " fid = "+ fid;
		}
		where += " order by projectid asc";
		pu.doList(TProject.class, where);
		//创造json数据对象
		return pu;
	}
	
	@Override
	public String doAddact(TProject obj) throws Exception {
		return baseDao.doAddObj(obj);
	}

	@Override
	public String doEditact(TProject obj, int id) throws Exception {
		return baseDao.doUpdateObj(TProject.class, id, obj);
	}
	
	@Override
	public String doDelact(int id) throws Exception {
		return baseDao.doDelObj(TProject.class, id);
	}

	@Override
	public TProject doLoad(int projectid) throws Exception {
		return baseDao.doLoadObj(TProject.class, projectid);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public String doListProjectForTree() throws Exception {
		List<Tree> treeList = new ArrayList<Tree>();
		List<TProject> list = Jdb.get().query(TProject.class,"fid=0 order by projectid");
		Tree tree = new Tree();
		tree.setId("0");
		tree.setText("全部项目");
		tree.setIconCls("icon-myjob");
		tree.setChildren(treeList(list));
		treeList.add(tree);
		return JSON.toJSONString(treeList);
	}
	
	@SuppressWarnings("unchecked")
	public List<Tree> treeList(List<TProject> list){
		List<Tree> treeList = new ArrayList<Tree>();
		for (TProject tProject : list) {
			Tree tree = new Tree();
			tree.setId(tProject.getProjectid()+"");
			tree.setText(tProject.getName());
			if(!tProject.getChildren().isEmpty()){
				tree.setChildren(treeList(tProject.getChildren()));
			}
			treeList.add(tree);
		}
		return treeList;
	}
}
